/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：处理连接协议
* CreateTime：2020-08-16 18:42:25
* Version：1.0
* Modify Recorder：
*************************************************/

namespace TinyToolKit.Network
{
    public class HandleConnMsg:HandleMsgBase
    {
        public HandleConnMsg(TcpServerHelper server) : base(server)
        {
            _requestType = RequestType.Conn;
        }
        
        //心跳处理
        public void MsgHeartBeat(Conn conn,ProtocolBase protocol)
        {
            conn.lastTickTime = TinyUtility.Time.GetTimeStamp();
        }

        public void MsgLogin(Conn conn, ProtocolBase protoBase)
        {
            Player player = conn.player;
            
            ProtocolBytes prot = new ProtocolBytes();
            prot.AddString("Login");
            
            //TODO 检测登陆的条件是否满足
            
            PlayerConnEvent connEvent = new PlayerConnEvent();
            connEvent.OnLogin(conn.player);
            prot.AddInt(1);
            player.Send(prot);
        }
        
        public void MsgLogout(Conn conn, ProtocolBase protoBase)
        {
            Player player = conn.player;
            
            ProtocolBytes prot = new ProtocolBytes();
            prot.AddString("Logout");
            prot.AddInt(1);

            if (player == null)
            {
                conn.Send(prot.Encode());
                conn.Close();
            }
            else
            {
                PlayerConnEvent connEvent = new PlayerConnEvent();
                connEvent.OnLogout(conn.player);
                
                player.Send(prot);
                player.Logout();
            }
        }
    }
}
